package com.cskaoyan.utils;

import com.cskaoyan.bean.Feedback;
import com.cskaoyan.bean.Log;
import com.cskaoyan.mapper.FeedbackMapper;
import com.cskaoyan.mapper.LogMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
@Aspect
public class LogAspect {
    @Autowired
    LogMapper logMapper;
    @Autowired
    FeedbackMapper feedbackMapper;

    @Pointcut("execution(public com.cskaoyan.vo.BaseRespVo com.cskaoyan.controller.AuthController.login(..))")
    public void weblogin(){}

    @After("weblogin()")
    public void loginafter (JoinPoint joinPoint){
//    登录成功写入数据库的值
        Log log = new Log();
        log.setAdmin("admin123");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr(); // 获取IP地址
        System.out.println(ip);
        System.out.println("测试能否正常进入");
        log.setIp(ip);
        log.setType(1);
        log.setAction("登录");
        log.setStatus(true);
        log.setResult("");
        log.setComment("");
        Date time = new Date(System.currentTimeMillis());
        log.setAddTime(time);
        log.setUpdateTime(time);
        log.setDeleted(false);
        logMapper.insertSelective(log);
    }

    @Pointcut("execution(public com.cskaoyan.vo.BaseRespVo com.cskaoyan.controller.wx.lsj.GoodsControllerLi.logout(..))")
    public void weblogout(){}

    @After("weblogout()")
    public void logoutafter (JoinPoint joinPoint){
//    登录成功写入数据库的值
        Log log = new Log();
        log.setAdmin("admin123");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr(); // 获取IP地址
      //  System.out.println(ip);
    //    System.out.println("测试能否正常进入");
        log.setIp(ip);
        log.setType(1);
        log.setAction("退出");
        log.setStatus(true);
        log.setResult("");
        log.setComment("");
        Date time = new Date(System.currentTimeMillis());
        log.setAddTime(time);
        log.setUpdateTime(time);
        log.setDeleted(false);
        logMapper.insertSelective(log);
    }

    @Pointcut("execution(public com.cskaoyan.vo.BaseRespVo com.cskaoyan.controller.AdminController.update(com.cskaoyan.bean.Admin))")
    public void webupdate(){}

    @After("webupdate()")
    public void updateafter (JoinPoint joinPoint){
//    登录成功写入数据库的值
        Log log = new Log();
        log.setAdmin("admin123");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr(); // 获取IP地址
        log.setIp(ip);
        log.setType(1);
        log.setAction("编辑管理员");
        log.setStatus(true);
        log.setResult("");
        log.setComment("");
        Date time = new Date(System.currentTimeMillis());
        log.setAddTime(time);
        log.setUpdateTime(time);
        log.setDeleted(false);
        logMapper.insertSelective(log);

    }

    @Pointcut("execution(public com.cskaoyan.vo.BaseRespVo com.cskaoyan.controller.AdminController.delete(com.cskaoyan.bean.Admin))")
    public void webdelete(){}

    @After("webdelete()")
    public void deleteafter (JoinPoint joinPoint){
//    登录成功写入数据库的值
        Log log = new Log();
        log.setAdmin("admin123");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr(); // 获取IP地址
        log.setIp(ip);
        log.setType(1);
        log.setAction("删除管理员");
        log.setStatus(true);
        log.setResult("");
        log.setComment("");
        Date time = new Date(System.currentTimeMillis());
        log.setAddTime(time);
        log.setUpdateTime(time);
        log.setDeleted(false);
        logMapper.insertSelective(log);
    }

@Pointcut("execution(public com.cskaoyan.vo.BaseRespVo com.cskaoyan.controller.AdminController.create(com.cskaoyan.bean.Admin))")
public void webcreate(){}

    @After("webcreate()")
    public void createafter (JoinPoint joinPoint){
//    登录成功写入数据库的值
        Log log = new Log();
        log.setAdmin("admin123");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr(); // 获取IP地址
        log.setIp(ip);
        log.setType(1);
        log.setAction("添加管理员");
        log.setStatus(true);
        log.setResult("");
        log.setComment("");
        Date time = new Date(System.currentTimeMillis());
        log.setAddTime(time);
        log.setUpdateTime(time);
        log.setDeleted(false);
        logMapper.insertSelective(log);
    }


    @Pointcut("execution(public com.cskaoyan.vo.BaseRespVo com.cskaoyan.controller.wx.lsj.GoodsControllerLi.detail(..))")
    public void detail(){}

    @After("detail()")
    public void detailafter (JoinPoint joinPoint){
//    新建一个

        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
            System.out.println((int)arg);
            Feedback feedback = new Feedback();

        }
        System.out.println("参数值为"+args);

    }

}
